package com.levi.mall.controller;

import com.levi.constants.MallConstants;
import com.levi.form.CartAddForm;
import com.levi.form.CartUpdateForm;
import com.levi.mall.service.CartService;
import com.levi.pojo.User;
import com.levi.vo.CartVo;
import com.levi.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * @author shentong
 * @since 2022/3/30 10:08 PM
 */
@Api(tags = "购物车接口")
@RestController
@RequestMapping("/carts")
public class CartController extends BaseController{
    @Autowired
    private CartService cartService;


    @GetMapping()
    @ApiOperation("购物车列表")
    public ResponseVo<CartVo> cartList(HttpSession session) {
        User user = getCurrentUser(session);
        return cartService.cartList(user.getId());
    }

    @PostMapping()
    @ApiOperation("添加购物车")
    public ResponseVo<CartVo> add(@Valid @RequestBody CartAddForm cartAddForm,
                                  HttpSession session) {
        User user = getCurrentUser(session);
        return cartService.add(user.getId(),cartAddForm);
    }

    @PutMapping("/{productId}")
    @ApiOperation("修改购物车")
    public ResponseVo<CartVo> update(@PathVariable("productId") Integer productId,
                                     @Valid @RequestBody CartUpdateForm form, HttpSession session) {
        User user = getCurrentUser(session);
        return cartService.updateCart(user.getId(), productId, form);
    }


    @DeleteMapping("/{productId}")
    @ApiOperation("删除购物车中的商品")
    public ResponseVo<CartVo> delete(@PathVariable Integer productId, HttpSession session) {
        User user = getCurrentUser(session);
        return cartService.deleteCart(user.getId(), productId);
    }


    @PutMapping("/selectAll")
    @ApiOperation("购物车全选")
    public ResponseVo<CartVo> selectAll(HttpSession session) {
        User user = getCurrentUser(session);
        return cartService.selectAll(user.getId());
    }


    @PutMapping("/unSelectAll")
    @ApiOperation("购物车全不选")
    public ResponseVo<CartVo> unSelectAll(HttpSession session) {
        User user = getCurrentUser(session);
        return cartService.unSelectAll(user.getId());
    }
    @GetMapping("/products/sum")
    @ApiOperation("购物车中所有商品的数量")
    public ResponseVo<Integer> sum(HttpSession session) {
        User user = getCurrentUser(session);
        return cartService.sum(user.getId());
    }

}
